package my.sweep_tchap;
/**
 *
 * @author Tyler Chapman
 * @version 0.1
 * 
 * This is the final page the user should get to. They will be able
 * to see the times that the street will be swept and they are given 
 * the option to set an alarm. If they choose to set an alarm a select 
 * wheel will pop up, and the user can select a time to be alerted to 
 * move their car. After they select a time to be alerted an alarm is 
 * triggered and the user will confirm that the alarm is set.
 * 
 */

import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.NSDate;
import org.xmlvm.iphone.UIAlertView;
import org.xmlvm.iphone.UIButton;
import org.xmlvm.iphone.UIButtonType;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UIControl;
import org.xmlvm.iphone.UIControlDelegate;
import org.xmlvm.iphone.UIControlEvent;
import org.xmlvm.iphone.UIControlState;
import org.xmlvm.iphone.UIDatePicker;
import org.xmlvm.iphone.UIFont;
import org.xmlvm.iphone.UILabel;
import org.xmlvm.iphone.UIScreen;
import org.xmlvm.iphone.UISwitch;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewController;

public class AlarmPage extends UIViewController {
	private int streetCode;
	private NSDate sweepTime;
	
	public AlarmPage(int streetCode){
		this.setTitle("Time");
		this.streetCode = streetCode;
		setSweepTime();
	}
	
	public void setSweepTime(){
		/**
		 * Method that connects to the database and returns the time
		 * the street will be swept based off of the street code that 
		 * is sent from the StreetPage.
		 */
		connect dbConnection = new connect();
    	sweepTime = dbConnection.getDBSweepTime(streetCode);
	}
	
	private NSDate getSweepTime() { 
		return sweepTime;
	}
    
	public void loadView() {
		super.loadView();
		final NSDate time = this.getSweepTime();
		final UIView root = getView();
		UIColor back = UIColor.groupTableViewBackgroundColor;
		root.setBackgroundColor(back);
		UIFont font = UIFont.fontWithNameSize("AmericanTypewriter", 18);
		UILabel label1 = new UILabel(new CGRect(0, 20, UIScreen.mainScreen().getApplicationFrame().size.width, 40));
		UILabel label2 = new UILabel(new CGRect(0, 50, UIScreen.mainScreen().getApplicationFrame().size.width, 40));
		UILabel label3 = new UILabel(new CGRect(0, 190, 100, 40));
		dateParser dateDisp = new dateParser(this.getSweepTime());
		label1.setText("The street will be swept on");
		label1.setTextAlignment(1);
		label1.setBackgroundColor(back);
		label1.setFont(font);
		label2.setText(dateDisp.getDate());
		label2.setTextAlignment(1);
		label2.setFont(font);
		label2.setAdjustsFontSizeToFitWidth(true);
		label2.setBackgroundColor(back);	
		label3.setText("Set Alarm");
		label3.setTextAlignment(2);
		label3.setBackgroundColor(back);
		root.addSubview(label1);
		root.addSubview(label2);
		root.addSubview(label3);
		
        
		final UISwitch alarmSwitch = new UISwitch();
		alarmSwitch.setFrame(new CGRect(200, 200, alarmSwitch.getFrame().size.width, alarmSwitch.getFrame().size.height));
		alarmSwitch.setOn(false);
        
		alarmSwitch.addTarget(new UIControlDelegate() {		
			/*
			 * Switch to allow the user to set an alarm. When triggered the UIDatePicker pulls up.
			 */
			public void raiseEvent(UIControl sender, int eventType) {
				final UIDatePicker datePick = new UIDatePicker();
				datePick.setDatePickerMode(2);
				datePick.setMinuteInterval(15);
				//time correction to sync with the times that UIDatePicker at GMT
				double timeSeconds = time.timeIntervalSinceReferenceDate() + 28800;
				NSDate newTime = NSDate.dateWithTimeIntervalSinceReferenceDate(timeSeconds);
				datePick.setDate(newTime);
				datePick.setMaximumDate(newTime);
                
				CGRect outRect = new CGRect(0, UIScreen.mainScreen().getBounds().size.height, 0, 0);
				UIButton button = UIButton.buttonWithType(UIButtonType.RoundedRect);
				button.setTitle("Set Alarm", UIControlState.Normal);
				datePick.setFrame(outRect);
				button.setFrame(outRect);
                
				root.addSubview(datePick);	
				root.addSubview(button);
				UIView.beginAnimations("Fade In");
				UIView.setAnimationDuration(0.5);
				button.addTarget(new UIControlDelegate() {
					/*
					 * Triggered when the alarm is set. An alert is sent
					 *  to the screen for the user to confirm the alarm time.
					 */
					public void raiseEvent(UIControl uic, int i) {
						//time correction from UIDatePicker to get back to the user's time
						double tempSec = datePick.getDate().timeIntervalSinceReferenceDate() - 28800;
						NSDate tempTime = NSDate.dateWithTimeIntervalSinceReferenceDate(tempSec);
						dateParser alarmTime = new dateParser(tempTime);
						UIAlertView alarmSet = new UIAlertView(null,"Alarm is set for " + alarmTime.getDate() , null, "confirm");
						alarmSet.show();
					}
				}, UIControlEvent.TouchUpInside);
				if(alarmSwitch.isOn()){            
					datePick.setFrame(new CGRect(0, 140, UIScreen.mainScreen().getBounds().size.width,  datePick.getFrame().size.height));
					button.setFrame(new CGRect(40, 370, 240, 30));
					UIView.commitAnimations();
				}
			}
            
		}, UIControlEvent.ValueChanged);
		root.addSubview(alarmSwitch);
		
		setView(root);
	}
    
}
